package first.group.ssh.globalstate;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Map;

public class GlobalStateParser {

    private GlobalStateParser() {
    }

    private static final Map<String, GlobalState> STATES = Map.ofEntries();
    
    public static boolean isSupported(String name) {
        return STATES.containsKey(name);
    }

    public static GlobalState.RequestPacket parseRequest(String name, boolean wantReply, ByteBuffer buffer) throws IOException {
        var state = STATES.get(name);
        if (state == null) {
            return null;
        }
        
        var requestParser = state.getRequestParser();
        
        return requestParser.parse(wantReply, buffer);
    }

    public static GlobalState.RequestPacket parseSuccess(String name, boolean wantReply, byte[] payload) throws IOException {
        var state = STATES.get(name);
        if (state == null) {
            return null;
        }
        
        var successParser = state.getSuccessParser();
        
        return successParser.parse(wantReply, payload);
    }
}
